import wifi from '@ohos.wifi';
import socket from '@ohos.net.socket';
import router from '@system.router';
import prompt from '@system.prompt'
import {changePreState} from '../../common/utils/changePreState';
export default {
    data: {
        curPrescription:{},
        title: "",
        medInfoList:[
            {
                name:'A药',
                useNum:"10g",
                useType:"内服",
                num:"25片"
            },
            {
                name:'B药',
                useNum:"18g",
                useType:"外服",
                num:"666片"
            },
            {
                name:'D药',
                useNum:"18g",
                useType:"外服",
                num:"666片"
            }
        ],
        tcp:socket.constructTCPSocketInstance(),
        TAG:"YZJ ",
        remote_ip:"192.168.1.113"

    },
    onInit() {
        console.log("xxx---切换:"+JSON.stringify(this.curPrescription))
        this.creatScoket();

    },
    back(){
        router.back();
    },
    confirm(){
        let ID = this.curPrescription.ID;
        changePreState(ID,"已送达",(res)=>{
            console.log("xxx---更改订单状态结果：  "+ res)
        })
        let msg = "已送达"+ID
        this.sendMessage(msg)
        this.back();
    },
    showDialog() {
        let that = this;
        prompt.showDialog({
            title: '',
            message: '请再次确认药物是否全部送达',
            buttons: [
                {
                    text: '确认',
                    color: '#000000',
                },
                {
                    text: '取消',
                    color: '#000000',
                }
            ],
            success: function(data) {
                console.log('dialog success callback，click button : ' + data.index);
                let index = data.index
                if(index==0){
                    that.confirm();
                }
            },
            cancel: function() {
                console.log('dialog cancel callback');
            },
        })
    },
    async creatScoket(){
        var that = this;
        this.getIpAddress();
        this.tcp.bind({address:this.getIpAddress(), port:8080, family: 1}, err => {
            if (err) {
                console.info(this.TAG+'bind fail');
                this.title='bind fail'
                return;
            }
            this.title='bind success'
            console.info(this.TAG+'bind success');
        })
        this.tcp.on('message', value => {
            let buffer = value.message;
            let dataView = new DataView(buffer);
            let str = "";

            for (let i = 0;i < dataView.byteLength; ++i) {
                str += String.fromCharCode(dataView.getUint8(i))

            }

            console.info(this.TAG+"on connect recevied"+str );





        });
    },
    async sendMessage(msg){

        let promise = this.tcp.connect({ address: {address: this.remoteip, port: 8080, family: 1} , timeout: 6000});
        promise.then(() => {
            console.log(this.TAG+'connect success');
            this.tcp.send({
                data:msg
            },err => {
                if (err) {
                    console.log(this.TAG+'send fail');
                    return;
                }
                console.log(this.TAG+'send success');
            })
        }).catch(err => {
            console.log(this.TAG+'connect fail');
        });
    },
    getIpAddress(){
        let ip=wifi.getIpInfo().ipAddress;
        this.localip = (ip >> 24 & 0xFF)+"."+ ((ip >> 16) & 0xFF)+"."+((ip >> 8) & 0xFF)+"."+(ip & 0xFF);
        console.info(this.TAG+this.localip);
    },
}
